(function($, win, doc) {
    'use strict';

    var $win = $(win), $doc = $(doc), $html = $doc.find('html'), $body;

    $.extend($, {
        isBoolean: function(b) {
            return typeof b === 'boolean';
        },
        isEmpty: function(obj) {
            return ((obj === undefined || obj === null)
                || (this.isString(obj) && this.trim(obj).length === 0)
                || ((this.isArray(obj) || this.isJq(obj)) && obj.length === 0)
                || (this.isPlainObject(obj) && this.collectKeys(obj).length === 0));
        },
        isJq: function(obj) {
            return !!(obj && obj.jquery);
        },
        jq: function(el) {
            return this.isJq(el) ? el : $(el);
        }
    });

    $.fn.extend({
        ellipsis: function(isMultiline, surfix) {
            if (!$.isBoolean(isMultiline)) {
                surfix = isMultiline;
                isMultiline = false;
            }
            surfix = surfix || '...';

            return this.each(function() {
                var $el = $.jq(this), html = $el.html(), $temp, fn;

                $el.after($temp = $el.clone(true).hide().css({
                    position: 'absolute',
                    width: isMultiline ? $el.width() : 'auto',
                    height: isMultiline ? 'auto' : $el.height(),
                    maxHeight: 'none',
                    overflow: 'visible'
                }));

                fn = isMultiline ? function() {
                    return $temp.height() > $el.height();
                } : function() {
                    return $temp.width() > $el.width();
                };

                while (html.length > 0 && fn()) {
                    html = html.substr(0, html.length - 1);
                    $temp.html(html + surfix);
                }
                $el.html($temp.html());
                $temp.remove();
            });
        }
    });
})(jQuery, window, document);

function replaceContent(li, index, data) {
	$(li).find('.boardTitle').text(data.title).ellipsis(true);
	$(li).find('.txt').text(data.content + "...");
	$(li).find(".date span:nth-child(2)").text(data.monthText + "." + data.day);
	$(li).find('.txt-box').wrap("<a href='" + data.link + "' target='_blank'></a>");
    $(li).find('.i_mg').css('background-image', 'url('+data.imgSrc+')');
}

function replaceSymbol(string){
	return string.replace(/&amp;/g, "&").replace(/[a-z|A-Z]+;/g,"");
}

var monthArr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var rssUrl = {};
if ($("html").attr('lang') === 'ko') {
	// rssUrl.rss_url = 'https://brunch.co.kr/rss/@@3Y27';
    rssUrl.rss_url = 'https://medium.com/feed/helloiconworld'
} else {
	rssUrl.rss_url = 'https://medium.com/feed/helloiconworld'
}

$.get('https://api.rss2json.com/v1/api.json', rssUrl, function (response) {

	if (response.status == 'ok') {
		const $blogSectionLiElemList = $("[blogSection]").find("li");
		$.each(response.items, function (k, item) {
			var rssData = {};

			//旮€ 鞓Π 雮犾
			var monthText = monthArr[Number(item.pubDate.split("-")[1]) - 1];
			var day = item.pubDate.split("-")[2].substr(0, 2);
			//img 旮侅柎鞓る姅 攵€攵�(brunch電� 鞎堧惃)
			var tagIndex = item.description.indexOf('<img'); 
			var srcIndex = item.description.substring(tagIndex).indexOf('src=') + tagIndex;
			var srcStart = srcIndex + 5;
			var srcEnd = item.description.substring(srcStart).indexOf('"') + srcStart;
			var src = item.description.substring(srcStart, srcEnd);//鞚措歆€ 旮侅柎鞓り赴 雭� p.backgroundImage()

			var yourString = item.description.replace(/<[^>]*>/g, "");
			var maxLength = 100;
			var trimmedString = yourString.substr(0, maxLength);
			trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));

			rssData.index = k;
			rssData.content = replaceSymbol(trimmedString);
			rssData.imgSrc = src;
			rssData.link = item.link;
			rssData.title = replaceSymbol(item.title);
			rssData.day = day;
			rssData.monthText = monthText;

			replaceContent($blogSectionLiElemList[k], k, rssData);
			return k < 8;
		});
	}
});